<template>
  <div class="addUserList">
    <el-dialog :visible.sync="dialogFormVisible" width="600px" :center="true">
      <div slot="title" class="title">新增用户</div>

      <el-form :model="form" label-width="110px" :rules="rules" ref="form">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="role_id">
          <el-select v-model="form.role_id">
            <el-option v-for="(value, key, index) in $store.state.roleObj" :key="index" :value="+key" :label="value"></el-option>
            <!-- <el-option :value="2" label="管理员"></el-option>
            <el-option :value="3" label="老师"></el-option>
            <el-option :value="4" label="学生"></el-option> -->
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status">
            <el-option :value="1" label="启用"></el-option>
            <el-option :value="0" label="禁用"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户备注" prop="remark">
          <el-input v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addUserList, editUserList } from "@/api/userList.js";
export default {
  props: ["mode"],
  data() {
    return {
      dialogFormVisible: false,
      form: {
        username: '',
        email: '',
        phone: '',
        role_id: '',
        status: '',
        remark: ""
      },
      rules: {
        username: [{ required: true, message: "请填入用户名", trigger: "change" }],
        email: [
          { required: true, message: "请填写用户邮箱", trigger: "change" },
          { validator: (rule, value,callback) => {
            let _reg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
            if(_reg.test(value)){
              callback()
            }else{
              callback('请输入正确的邮箱地址')
            }
          }, trigger: 'change'}
        ],
        phone: [
          { required: true, message: "请填写用户电话", trigger: "change" },
          { validator: (rule, value, callback) => {
            let reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
            if(reg.test(value)){
              callback()
            }else{
              callback('请输入正确的电话号码')
            }
          }, trigger: 'change'}
        ],
        role_id: [
          { required: true, message: "请选择你的角色", trigger: "change" }
        ]
      }
    };
  },

  watch: {
    dialogFormVisible(newVal) {
      if (newVal == false) {
        // 清除表单及验证
        this.$refs.form.resetFields();
      }
    }
  },

  methods: {
    submit() {
      // 全局验证
      this.$refs.form.validate(result => {
        if (result) {
          // 新增
          if (this.mode == "add") {
            addUserList(this.form).then(res => {
              console.log("用户新增", res);
              this.$message.success("用户新增成功");
              // 关闭对话框
              this.dialogFormVisible = false;
              // 刷新数据
              this.$emit("input");
            });
          }else {
            // 编辑
            editUserList(this.form).then(res => {
              console.log('用户编辑', res);
              this.$message.success('编辑成功')
              this.dialogFormVisible = false
              this.$emit('input')
            })
          }
        }
      });
    }
  }
};
</script>

<style lang="less">
.addUserList {
  .el-dialog__header {
    padding: 0;
  }

  .title {
    height: 53px;
    line-height: 53px;
    background: linear-gradient(to right, #01c5fa, #1394fa);
    color: #fff;
    font-size: 18px;
    font-weight: bold;
  }

  .el-dialog__headerbtn .el-dialog__close {
    color: #fff;
    font-size: 12px;
  }
}
</style>